﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WanDa.Model;
using WanDa.IBll;
using WanDa.BllContainer;
using Demo.Controllers;
using System.IO;

namespace WanDa.UI.Controllers
{
    public class WanDaController : BaseController
    {
        //
        // GET: /WanDa/
        private IUsersBll usersbll = Container.resolve<IUsersBll>();
        private IProjectAppBll projectappbll = Container.resolve<IProjectAppBll>();
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.username = LoginInfo.username;
            return View();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public ActionResult UserLogin()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UserLogin2(users model)
        {
            List<users> list = usersbll.loginsel(model.username);
            if (list == null || list.Count == 0)
            {
                return Content("<script>alert('用户名不存在');location.href='/wanda/userlogin'</script>");
            }
            else
            {
                if (model.userpwd == list[0].userpwd)
                {
                    WriteDataToCookie(model);
                    return Content("<script>alert('成功');location.href='/wanda/index'</script>");
                }
                else
                {
                    return Content("<script>alert('密码错误');location.href='/wanda/userlogin'</script>");
                }
            }
        }
        /// <summary>
        /// 项目立项
        /// </summary>
        /// <returns></returns>
        public ActionResult lixiang()
        {
            return View();
        }
        [HttpPost]
        public ActionResult lixiang2(HttpPostedFileBase file1, HttpPostedFileBase file2)
        {
            string path = Server.MapPath("/Files/");
            string filename = Path.GetFileName(file1.FileName);
            string filename2 = Path.GetFileName(file2.FileName);
            string filepath = path + filename;
            string filepath2 = path + filename2;
            file1.SaveAs(filepath);
            file2.SaveAs(filepath2);
            ProjectApp pro = new ProjectApp();
            pro.ProUrl = filepath;
            pro.ProFileName = filename;
            pro.ProSolUrl = filepath2;
            pro.ProSolFileName = filename2;
            if (projectappbll.Add(pro))
            {
                return Content("<script>alert('提交成功');location.href='/wanda/lixiang'</script>");
            }
            else
            {
                return Content("<script>alert('提交失败');location.href='/wanda/lixiang'</script>");
            }
        }
        /// <summary>
        /// 可行性研究
        /// </summary>
        /// <returns></returns>
        public ActionResult KeYan()
        {
            return View();
        }
    }
}